import sys, serial,threading

def extractdata(data):
    data = data.strip()
    return data.split(' ')[0]

def writer():
    global se,fh
    i = 0
    while True:
        i = i + 1
        data = se.readline()
        data = extractdata(data)
        if data == '0.0000':
            print '======'
            data = ''
            i = 0
        else:
            print '%i\t'%i, data
        data = data + '\r\n'
        fh.write(data)
        fh.flush()
    
se = serial.Serial(0)

fh = open(sys.argv[1], 'w')
t = threading.Thread(target=writer)
t.setDaemon(True)
t.start()

while True:
    pass
